package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.market.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListResponse;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostResponse;
import com.gzhryc.net_marketing.core.models.LogisticsCostItem;
import com.gzhryc.net_marketing.supplier.Supplier;
import com.gzhryc.net_marketing.supplier.SupplierManager;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCostConfig;
import com.gzhryc.net_marketing.supplier.config.services.SupplierLogisticsCostConfigService;
import com.gzhryc.net_marketing.supplier.config.services.dto.SupplierLogisticsCostConfigSearch;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.ELogisticsAreaScope;

public class LogisticsAPIController {

    static Logger log = Logger.getLogger(LogisticsAPIController.class);

    @Route(value = { "/supplier_api/v1/{supplierId}/logistics/cost/find.data" })
    public LogisticsCostListResponse findLogistics(HttpServletRequest request) {
        LogisticsCostListResponse apiResp = new LogisticsCostListResponse(DefaultAPICodeConstants.EXECUTE_ERROR, "获取物流收费列表失败");
        LogisticsCostListRequest apiReq = WebUtils.getJsonRequest(request, LogisticsCostListRequest.class);
        Long supplierId = WebUtils.getLong(request, "supplierId");
        if (apiReq != null && supplierId != null) {
            log.info("获取{{0}}供应商物流收费列表，请求：{{1:json}}",supplierId,apiReq);
            if (SupplierManager.setCurrentSupplierId(supplierId)) {
                try {
                	Supplier supplier = SupplierManager.getSupplier();
                	if(supplier != null) {
	                    List<SupplierLogisticsCostConfig> logisticsCostConfigList = null;
	                    if(StringTools.isNotBlank(apiReq.getBody().getProvince())){
	                        SupplierLogisticsCostConfigSearch search = new SupplierLogisticsCostConfigSearch();
	                        search.setAreaScope(ELogisticsAreaScope.Province.index());
	                        search.setProvince(apiReq.getBody().getProvince());
	                        logisticsCostConfigList = SupplierLogisticsCostConfigService.self().findJoinBySearch(search);
	                    }
	
	                    if(logisticsCostConfigList == null || logisticsCostConfigList.size() <= 0) {
	                        //如果设置了省市但未获取数据，则取全国
	                        SupplierLogisticsCostConfigSearch search = new SupplierLogisticsCostConfigSearch();
	                        search.setAreaScope(ELogisticsAreaScope.Country.index());
	                        logisticsCostConfigList = SupplierLogisticsCostConfigService.self().findJoinBySearch(search);
	                    }
	
	                    if(logisticsCostConfigList != null && logisticsCostConfigList.size() > 0) {
	                        apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
	                        apiResp.getBody().setItems(new ArrayList<>());
	                        for (SupplierLogisticsCostConfig logisticsCostConfig : logisticsCostConfigList) {
	                            LogisticsCostItem item = new LogisticsCostItem();
	                            item.setCompanyId(logisticsCostConfig.getCompanyId());
	                            item.setCompanyName(logisticsCostConfig.getCompanyName());
	                            item.setAreaScope(logisticsCostConfig.getAreaScope());
	                            item.setProvince(logisticsCostConfig.getProvince());
	                            item.setCity(logisticsCostConfig.getCity());
	                            item.setFreeMinMoney(logisticsCostConfig.getFreeMinMoney());
	                            item.setMoney(logisticsCostConfig.getMoney());
	                            apiResp.getBody().getItems().add(item);
	                        }
	                    }
                	}
                }catch (Exception e) {
                    log.error(e.getMessage(), e);
                }finally{
                    SupplierManager.removeCurrentSupplierId();
                }
            }
        }else{
            apiResp.setMsg("参数错误");
        }
        return apiResp;
    }

    @Route(value = { "/supplier_api/v1/{supplierId}/logistics/cost/usable.data" })
    public LogisticsUsableCostResponse getUsableLogistics(HttpServletRequest request) {
        LogisticsUsableCostResponse apiResp = new LogisticsUsableCostResponse(DefaultAPICodeConstants.EXECUTE_ERROR, "获取物流收费列表失败");
        LogisticsUsableCostRequest apiReq = WebUtils.getJsonRequest(request, LogisticsUsableCostRequest.class);
        Long supplierId = WebUtils.getLong(request, "supplierId");
        if (apiReq != null && supplierId != null) {
            log.info("获取{{0}}供应商可用物流收费，请求：{{1:json}}",supplierId,apiReq);
            if (SupplierManager.setCurrentSupplierId(supplierId)) {
                try {
                	Supplier supplier = SupplierManager.getSupplier();
                	if(supplier != null) {
	                    SupplierLogisticsCostConfig logisticsCostConfig = SupplierLogisticsCostConfigService.self(supplier.getJdbcKey())
	                    		.getUsableConfig(apiReq.getBody().getCompanyId(),apiReq.getBody().getProvince());
	                    if(logisticsCostConfig != null){
	                        LogisticsCostItem item = new LogisticsCostItem();
	                        item.setCompanyId(logisticsCostConfig.getCompanyId());
	                        item.setCompanyName(logisticsCostConfig.getCompanyName());
	                        item.setAreaScope(logisticsCostConfig.getAreaScope());
	                        item.setProvince(logisticsCostConfig.getProvince());
	                        item.setCity(logisticsCostConfig.getCity());
	                        item.setFreeMinMoney(logisticsCostConfig.getFreeMinMoney());
	                        item.setMoney(logisticsCostConfig.getMoney());
	
	                        apiResp.setCode(DefaultAPICodeConstants.SUCCESS, null);
	                        apiResp.setBody(item);
	                    }
                	}
                }catch (Exception e) {
                    log.error(e.getMessage(), e);
                }finally{
                    SupplierManager.removeCurrentSupplierId();
                }
            }
        }else{
            apiResp.setMsg("参数错误");
        }
        return apiResp;
    }
}
